/*
 * Copyright (C) 2003, Velocityme Partnership
 * DepartmentBean.java
 *
 * Created on 25 August 2003, 04:50
 */

package com.velocityme.entity;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.DepartmentValue;

/**
 *
 * @author  Robert Crida Work
 * @ejb.bean
 *           type="CMP"
 *           cmp-version="2.x"
 *           name="Department"
 *           jndi-name="ejb/Department"
 *           view-type="local"
 * @ejb.transaction type="Required"
 *
 * @ejb.value-object match="*"
 *
 * @ejb.persistence table-name="department"
 *
 * @ejb.util generate="physical"
 *
 * @ejb.finder signature="java.util.Collection findAll()"
 *              query="SELECT OBJECT(o) FROM Department o WHERE o.contactableLocal.nodeLocal.dBObjectLocal.isDeleted = FALSE"
 *
 * @jboss.persistence create-table="true"
 *                    remove-table="false"
 */
public abstract class DepartmentBean implements EntityBean {
    
    /**
    * Context set by container
    */
    private javax.ejb.EntityContext m_entityContext;

    public void setEntityContext(javax.ejb.EntityContext entityContext) {
        m_entityContext = entityContext;
    }

    public void unsetEntityContext() {
        m_entityContext = null;
    }
  
    /** @ejb.create-method */
    public com.velocityme.interfaces.DepartmentPK ejbCreate(com.velocityme.interfaces.ContactableLocal p_contactableLocal,
                                                            com.velocityme.valueobjects.DepartmentValue p_value) throws CreateException {
        setDepartmentId(p_contactableLocal.getContactableId());
        // EJB 2.0 spec says return null for CMP ejbCreate methods.
        return null;
    }

    public void ejbPostCreate(com.velocityme.interfaces.ContactableLocal p_contactableLocal,
                              com.velocityme.valueobjects.DepartmentValue p_value) throws CreateException {
        // CMR relations must be set in ejbPostCreate
        setDepartmentValue(p_value);
        setContactableLocal(p_contactableLocal);
    }

    /**
     *  This gets a local object from a remote object.
     *  @ejb.interface-method view-type="remote"
     */
    public com.velocityme.interfaces.DepartmentLocal getLocal() {
        return (DepartmentLocal)m_entityContext.getEJBLocalObject();
    }
    
    /** @ejb.interface-method view-type="local" */
    public abstract com.velocityme.valueobjects.DepartmentValue getDepartmentValue();
    /** @ejb.interface-method view-type="local" */
    public abstract void setDepartmentValue(com.velocityme.valueobjects.DepartmentValue p_value);
    
    /** @ejb.interface-method view-type="local" */
    public com.velocityme.valueobjects.DepartmentValue getDepartmentValueQualified() {
        try {
            DepartmentValue departmentValue = new DepartmentValue(getDepartmentId(), getQualifiedName());
            if (getContactableLocal() != null)
               departmentValue.setContactableValue(getContactableLocal().getContactableValue());
            return departmentValue;
        }
        catch(Exception e) {
            throw new EJBException(e);
        }
    }
    
    //=======================================================================
    //
    //                   ATTRIBUTES
    //
    //=======================================================================
      
    /**
     * @ejb.persistence column-name="departmentId"
     * @ejb.interface-method view-type="local"
     * @ejb.pk-field 
     */
    public abstract java.lang.Integer getDepartmentId();
    /** @ejb.interface-method view-type="local" */
    public abstract void setDepartmentId(java.lang.Integer userId);

    //=======================================================================
    //
    //                   RELATIONSHIPS
    //
    //=======================================================================
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="Department-Contactable"
     *               role-name="Department-has-a-Contactable"
     * @jboss.relation fk-column="contactableIdFk"
     *                 related-pk-field="contactableId"
     * @ejb.value-object match="*" 
     *                   aggregate="com.velocityme.valueobjects.ContactableValue"
     *                   aggregate-name="ContactableValue"
     */
    public abstract com.velocityme.interfaces.ContactableLocal getContactableLocal();
    /** @ejb.interface-method view-type="local" */
    public abstract void setContactableLocal(com.velocityme.interfaces.ContactableLocal contactableLocal);
    
    //=======================================================================
    //
    //                   MANAGEMENT METHODS
    //
    //=======================================================================
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.value-object match="*" 
     */
    public java.lang.String getQualifiedName() {
        NodeLocal nodeLocal = getContactableLocal().getNodeLocal();
        String qualifiedName = nodeLocal.getName();
        nodeLocal = nodeLocal.getParentNodeLocal();
        for (; nodeLocal.getFinalType().equals("Department") || nodeLocal.getFinalType().equals("Organisation"); nodeLocal = nodeLocal.getParentNodeLocal()) {
            qualifiedName = nodeLocal.getName() + ":" + qualifiedName;
        }
        return qualifiedName;
    }
    
}
